from jili.core.convert import str2datetime
def get_monthstr(d):
    month0 = str(d.month)
    month0=(2-len(month0))*"0"+month0
    return month0
def monthstr(month):
    month0 = str(month)
    month0 = (2 - len(month0)) * "0" + month0
    return month0
def calc_month(month,diff):
    month0=int(month)+diff
    while month0>12:
        month0 = month0 - 12
    while month0<1:
        month0 = month0 + 12
    return monthstr(month0)
def get_nearmonth(d, limitdays=20):
    d = str2datetime(d)
    month0 = get_monthstr(d)
    if d.day > limitdays:
        n1=calc_month(month0,2)
        n2 = calc_month(month0, 3)
    else:
        n1 = calc_month(month0, 1)
        n2 = calc_month(month0, 2)
    return n1,n2
def get_mastermonth(months,master):
    n=months.index(master)
    m=months[n:n+3]
    nn=3-len(m)
    if nn>0:
        m=m+months[:nn]
    return m

if __name__=="__main__":
    print(get_mastermonth(['01', '05', '09'],"01"))
    print(get_mastermonth(['01', '05', '09'], "05"))
    print(get_mastermonth(['01', '05', '09'], "09"))
    print(get_mastermonth(["01","02","03","04","05","06","07","08","09","10","11","12"], "01"))
    print(get_mastermonth(["01","02","03","04","05","06","07","08","09","10","11","12"], "05"))
    print(get_mastermonth(["01","02","03","04","05","06","07","08","09","10","11","12"], "11"))
    # print(get_nearmonth("20231119"))
    # print(get_nearmonth("20231120"))
    # print(get_nearmonth("20231121"))
    # print(get_nearmonth("20231125"))
    # print(get_nearmonth("20231101"))